#!/bin/bash
set -xeuo pipefail
set -o errexit

:<<EOF
https://www.elastic.co/guide/en/elasticsearch/plugins/7.10/repository-hdfs.html
https://www.elastic.co/guide/en/elasticsearch/plugins/7.8/repository-hdfs.html

docker exec -u root -it elasticsearch bash -c "elasticsearch-plugin list"
sudo bin/elasticsearch-plugin remove repository-hdfs

HDFS Kerberos出现的问题 https://risdenk.github.io/2017/12/23/elasticsearch-hdfs-snapshot-repository-java-security-manager-kerberos.html#:~:text=The%20Elasticsearch%20HDFS%20Snapshot%20Repository%20provides%20the%20ability,into%20Elasticsearch%20proper%20but%20was%20still%20a%20plugin.

EOF

# 先启动了ES 
# ./start.sh 8snap
docker exec -u root -it elasticsearch bash -c "elasticsearch-plugin list"| grep hdfs ||(
  docker cp servers/pkg/repository-hdfs-7.8.0.zip elasticsearch2:/opt
  docker cp servers/pkg/repository-hdfs-7.8.0.zip elasticsearch:/opt

#手工确认下y
  docker exec -u root -it elasticsearch bash -c "elasticsearch-plugin install file:///opt/repository-hdfs-7.8.0.zip"
  docker exec -u root -it elasticsearch2 bash -c "elasticsearch-plugin install file:///opt/repository-hdfs-7.8.0.zip"

#如果存在repository-hdfs,表明已经安装好了
  docker exec -u root -it elasticsearch bash -c "elasticsearch-plugin list"
  docker exec -u root -it elasticsearch2 bash -c "elasticsearch-plugin list"
  # repository-hdfs-7.8.0.zip 解压到 /usr/share/elasticsearch/plugins/repository-hdfs
)

#建立测试使用的目录的设置权限
docker exec -ti hdfs-namenode hdfs dfs -mkdir /elastic_hdfs_repository || true
# 777权限仅限测试，生产中需要设置对于elasticsearch用户[es容器的运行用户]的权限
docker exec -ti hdfs-namenode hdfs dfs -chmod 777 /elastic_hdfs_repository
docker exec -ti hdfs-namenode hdfs dfs -ls /

# 配置文档 https://www.elastic.co/guide/en/elasticsearch/plugins/master/repository-hdfs.html
# https://stackoverflow.com/questions/62591098/how-to-connect-hdfs-ha-for-elasticsearch-snapshot
# curl -XPUT http://localhost:9200/_snapshot/hdfs?pretty -H 'Content-Type: application/json' -d'
# {
#     "type" : "hdfs",
#     "settings" : {
#       "dfs" : {
#         "http" : {
#           "policy" : "HTTPS_ONLY"
#         }
#       },
#       "path" : "/elastic_hdfs_repository",
#       "conf" : {
#         "dfs" : {
#           "client" : {
#             "failover" : {
#               "proxy" : {
#                 "provider" : {
#                   "my-cluster-nameservice" : "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider"
#                 }
#               }
#             }
#           },
#           "ha" : {
#             "automatic-failover" : {
#               "enabled" : {
#                 "my-cluster-nameservice" : "true"
#               }
#             },
#             "namenodes" : {
#               "my-cluster-nameservice" : "namenode1,namenode2"
#             }
#           },
#           "data" : {
#             "transfer" : {
#               "protection" : "privacy"
#             }
#           },
#           "namenode" : {
#             "rpc-address" : {
#               "my-cluster-nameservice" : {
#                 "namenode1" : "nn1.domain.com:8020",
#                 "namenode2" : "nn2.domain.com:8020"
#               }
#             }
#           },
#           "nameservices" : "my-cluster-nameservice"
#         },
#         "fs" : {
#           "defaultFS" : "hdfs://elastic_hdfs_repository",
#           "hdfs" : {
#             "impl" : "org.apache.hadoop.hdfs.DistributedFileSystem"
#           }
#         },
#         "hadoop.http.authentication.token.validity": 36000
#       },
#       "security" : {
#         "principal" : "elasticsearch/_HOST@DOMAIN.COM"
#       },
#       "uri" : "hdfs://my-cluster-nameservice"
#     }
# }
# '

#官方论坛的一个设置 https://discuss.elastic.co/t/hdfs-respository-issue/116465
# shortcircuit和dfs.domain.socket.path需要同时设置
curl -XPUT http://localhost:9200/_snapshot/hdfs?pretty -H 'Content-Type: application/json' -d'
{
  "type": "hdfs",
  "settings": {
    "uri": "hdfs://172.19.0.10:8020/",
    "path": "/elastic_hdfs_repository",
    "conf.dfs.client.read.shortcircuit": "true",
    "conf.dfs.domain.socket.path": "/var/run/hdfs-sockets"
  }
}
'

echo "done"